package com.hemaapp.yjnh.wxapi;

import xtom.frame.util.XtomSharedPreferencesUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.hemaapp.BaseConfig;
import com.hemaapp.yjnh.R;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

	private IWXAPI api;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wxpayentry);

		api = WXAPIFactory.createWXAPI(this, BaseConfig.APPID_WEIXIN);
		api.handleIntent(getIntent(), this);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		//0成功，展示成功页面
		//-1错误，可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
		//-2用户取消，无需处理。发生场景：用户不支付了，点击取消，返回APP。
		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
		XtomSharedPreferencesUtil.save(this,"weixinpay","1");
		switch(resp.errCode){
		case 0:   //支付成功
			XtomSharedPreferencesUtil.save(this,"status","0");
			break;
		case -1:  //支付失败
			XtomSharedPreferencesUtil.save(this,"status","-1");
			break;
		case -2:  //用户取消
			XtomSharedPreferencesUtil.save(this,"status","-2");
			break;
		}
		finish();
	}
}